package name.bingo.utils;

import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.CharArrayBuffer;
import org.apache.http.util.EntityUtils;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 *
 *
 *
 */
public class HttpUtil {

    public static String doPost(String urlStr, String poststr) {
        URL connect;
        StringBuffer data = new StringBuffer();
        try {
            connect = new URL(urlStr);
            HttpURLConnection connection = (HttpURLConnection) connect.openConnection();
            connection.setRequestMethod("POST");
            connection.setDoOutput(true);
            connection.setReadTimeout(2500);
            connection.setRequestProperty("Content-Type", "application/json");
            OutputStreamWriter paramout = new OutputStreamWriter(connection.getOutputStream(), "UTF-8");
            paramout.write(poststr);
            paramout.flush();
            InputStream inputStream = connection.getInputStream();
            InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "UTF-8");
            BufferedReader reader = new BufferedReader(inputStreamReader);

            String line;
            while ((line = reader.readLine()) != null) {
                data.append(line);
            }
            paramout.close();
            reader.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return data.toString();
    }
    public static String doGet(String url){
         String result = null;
         CloseableHttpClient httpClient = HttpClients.createDefault();
         HttpGet get = new HttpGet(url);
         CloseableHttpResponse response = null;
         try {
             response = httpClient.execute(get);
             if(response != null && response.getStatusLine().getStatusCode() == 200) {
                 HttpEntity entity = response.getEntity();
                 result = entityToString(entity);
             }
             return result;
         } catch (IOException e) {
             e.printStackTrace();
         }finally {
            try {
                 httpClient.close();
                 if(response != null)
                     {
                         response.close();
                     }
            } catch (IOException e) {
                e.printStackTrace();
            }
         }
         return null;
    }
    private static String entityToString(HttpEntity entity) throws IOException {
         String result = null;
         if(entity != null)
             {
                 long lenth = entity.getContentLength();
                 if(lenth != -1 && lenth < 2048)
                     {
                         result = EntityUtils.toString(entity,"UTF-8");
                     }else {
                         InputStreamReader reader1 = new InputStreamReader(entity.getContent(), "UTF-8");
                         CharArrayBuffer buffer = new CharArrayBuffer(2048);
                         char[] tmp = new char[1024];
                         int l;
                         while((l = reader1.read(tmp)) != -1) {
                                 buffer.append(tmp, 0, l);
                             }
                         result = buffer.toString();
                     }
             }
         return result;
     }

}
